package cn.venny.doudian.service.impl;

import cn.venny.doudian.entity.AllAreas;
import cn.venny.doudian.entity.PddAllAreas;
import cn.venny.doudian.mapper.PddAllAreasMapper;
import cn.venny.doudian.model.bean.DoudianAddressAllAreas;
import cn.venny.doudian.service.IPddAllAreasService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 * 服务实现类
 * </p>
 * @author wdq
 * @since 2020-09-03
 */
@Service
public class PddAllAreasServiceImpl extends ServiceImpl<PddAllAreasMapper, PddAllAreas> implements IPddAllAreasService {


    @Override
    public void syncData(List<DoudianAddressAllAreas> maps) {
        if (CollectionUtils.isEmpty(maps)) {
            return;
        }

        List<AllAreas> allList = new CopyOnWriteArrayList<>();
        maps.forEach(m -> {
            System.out.println(JSON.toJSONString(m));
            List<DoudianAddressAllAreas.OneDistricts> subDistricts = m.getSubDistricts();
            if (CollectionUtils.isEmpty(subDistricts)) {
                return;
            }
            subDistricts.forEach(one -> {
                String name1 = one.getName();
                buildPddAllAreas(one.getCode(), one.getName(), m.getCode(), allList);
                List<DoudianAddressAllAreas.TwoDistricts> subDistricts1 = one.getSubDistricts();
                if (CollectionUtils.isEmpty(subDistricts1)) {
                    return;
                }
                subDistricts1.forEach(two -> {
                    String name2 = two.getName();
                    buildPddAllAreas(two.getCode(), two.getName(), one.getCode(), allList);
                    List<DoudianAddressAllAreas.ThreeDistricts> subDistricts2 = two.getSubDistricts();
                    if (CollectionUtils.isEmpty(subDistricts2)) {
                        return;
                    }
                    subDistricts2.forEach(three -> {
                        String name3 = three.getName();
                        buildPddAllAreas(three.getCode(), three.getName(), two.getCode(), allList);
                        List<DoudianAddressAllAreas.Fouristricts> subDistricts3 = three.getSubDistricts();
                        if (CollectionUtils.isEmpty(subDistricts3)) {
                            return;
                        }
                        subDistricts3.forEach(four -> {
                            buildPddAllAreas(four.getCode(), four.getName(), three.getCode(), allList);
                        });
                    });
                });
            });
        });

        System.out.println("==============================================================");
//        System.out.println(allList);

        List<PddAllAreas> list1 = new ArrayList<>();
        allList.forEach(all -> {
            System.out.println(all);
            PddAllAreas pddAllAreas = baseMapper.selectOne(new LambdaQueryWrapper<PddAllAreas>()
                    .eq(PddAllAreas::getNationalCode, all.getCode()).eq(PddAllAreas::getIsEnable, 1));
            if (pddAllAreas != null) {
                return;
            }
            PddAllAreas parent = baseMapper.selectOne(new LambdaQueryWrapper<PddAllAreas>()
                    .eq(PddAllAreas::getNationalCode, all.getParentCode()).eq(PddAllAreas::getIsEnable, 1));
            if (parent == null) {
                return;
            }
            pddAllAreas = new PddAllAreas();
            pddAllAreas.setIsEnable(1);
            pddAllAreas.setRegionName(all.getName());
            pddAllAreas.setRegionAliasName(all.getName());
            pddAllAreas.setParentId(parent.getId());
            pddAllAreas.setRegionType(parent.getRegionType() + 1);
            pddAllAreas.setNationalCode(all.getCode());
            pddAllAreas.setLasterUserId(0);
            pddAllAreas.setSort(0);
            pddAllAreas.setIsDel(0);
            list1.add(pddAllAreas);
        });

        saveBatch(list1);
        System.out.println(list1);
    }


    private void buildPddAllAreas(Long code, String name, Long parentCode, List<AllAreas> list) {
        if (code < 110000) {
            return;
        }
        AllAreas pa = new AllAreas();
        pa.setCode(String.valueOf(code));
        pa.setName(name);
        pa.setParentCode(String.valueOf(parentCode));
        list.add(pa);
    }
}